from typing import Optional

import PySide6.QtCore
import PySide6.QtWidgets
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import (QAbstractButton, QDialog, QDialogButtonBox,
                               QVBoxLayout, QWidget)

from ..DialogOACBase import DialogOACBase


class CustomDialog(QDialog):
    '''
    description:
        Dialog 的基类, 有3个Signal：
            - signalDone
            - signalAccept
            - signalReject
    '''
    signalApply = Signal(dict)
    signalOk = Signal(dict)
    signalCancel = Signal(dict)
    def __init__(
        self, 
        widget: 'DialogOACBase',
        parent: Optional[PySide6.QtWidgets.QWidget] = None, 
        f: PySide6.QtCore.Qt.WindowFlags = Qt.Dialog) -> None:
        ################################################
        # 设置类内数据 ==================================
        super().__init__(parent=parent, f=f)
        self.resize(widget.width(), widget.height())
        self._innerWidget = widget
        self._buttonBox = QDialogButtonBox(self)
        self._buttonBox.setObjectName(u"buttonBox")
        self._buttonBox.setOrientation(Qt.Horizontal)
        self._buttonBox.setStandardButtons(
            QDialogButtonBox.Cancel|
            QDialogButtonBox.Ok |
            QDialogButtonBox.Apply)
        self._verticalLayout = QVBoxLayout(self)
        self._verticalLayout.setObjectName(u"verticalLayout")
        self._verticalLayout.addWidget(self._innerWidget)
        self._verticalLayout.addWidget(self._buttonBox)
        # ==============================================

        # 设置信号槽 ====================================
        self._buttonBox.clicked.connect(self._SlotButtonBoxClicked)
        # ==============================================

        # 设置窗体表现 ==================================
        # ==============================================
    
    def _SlotButtonBoxClicked(self, button: 'QAbstractButton') -> None:
        '''
        function:
            当按下按钮的时候做什么
        '''
        if (self._buttonBox.standardButton(button) == QDialogButtonBox.Ok):
            # 按下 Ok 按钮后做什么
            if (hasattr(self._innerWidget, 'GetOk')):
                d_ = self._innerWidget.GetOk()
                if (len(d_) > 0):
                    self.signalOk.emit(d_)
            self.accept()
        elif (self._buttonBox.standardButton(button) == QDialogButtonBox.Apply):
            # 当按下 Apply 按钮的时候做什么
            if (hasattr(self._innerWidget, 'GetApply')):
                d_ = self._innerWidget.GetApply()
                if (len(d_) > 0):
                    self.signalApply.emit(d_)
        elif (self._buttonBox.standardButton(button) == QDialogButtonBox.Cancel):
            # 当按下 Reject 按钮的时候做什么
            if (hasattr(self._innerWidget, 'GetCancel')):
                d_ = self._innerWidget.GetCancel()
                if (len(d_) > 0):
                    self.signalCancel.emit(d_)
            self.reject()

